package com.example.limit.controller;

import com.example.limit.entity.RuleGroup;
import com.example.limit.mapper.RuleGroupMapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/rule-groups")
public class RuleGroupController {
    
    private final RuleGroupMapper groupMapper;
    
    public RuleGroupController(RuleGroupMapper groupMapper) {
        this.groupMapper = groupMapper;
    }
    
    @PostMapping
    public String createRuleGroup(@RequestBody RuleGroup group) {
        groupMapper.insert(group);
        return "规则组创建成功";
    }
    
    @PutMapping("/{id}")
    public String updateRuleGroup(@PathVariable Long id, @RequestBody RuleGroup group) {
        group.setId(id);
        groupMapper.update(group);
        return "规则组更新成功";
    }
    
    @DeleteMapping("/{id}")
    public String deleteRuleGroup(@PathVariable Long id) {
        groupMapper.delete(id);
        return "规则组删除成功";
    }
    
    @GetMapping("/{id}")
    public RuleGroup getRuleGroup(@PathVariable Long id) {
        return groupMapper.selectById(id);
    }
    
    @GetMapping
    public List<RuleGroup> getActiveRuleGroups() {
        return groupMapper.selectActiveGroups();
    }
}